package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.AbStrUtil;
import com.ab.util.ToastUtil;
import com.ab.view.clear_edit_text.ClearEditText;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.SchoolLogin;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

public class CurriculumScheduleLoginActivity extends AppBaseActivity implements OnMessageResponse {


    @Bind(R.id.ced_stu_no)
    ClearEditText cedStuNo;
    @Bind(R.id.cet_stu_pwd)
    ClearEditText cetStuPwd;
    private String mStuNo, mPwd;


    @Override
    public int bindLayout() {
        return R.layout.activity_curriculum_schedule_login;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.ke_cheng_biao);
        autoLogin();
    }

    @OnClick({R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.btn_login:
                login();
                break;
        }
    }

    /**
     * 登录
     */
    private void login() {
        if (checkTelPwd()) {
            loginPost();
        }
    }

    /**
     * 提交登登录信息
     */
    private void loginPost() {
        LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
        Api.stuLogin(this, mStuNo, mPwd);
    }

    /**
     * 检查用户名和密码是否为空
     */
    private boolean checkTelPwd() {

        mStuNo = getEidtTextValue(cedStuNo);
        mPwd = getEidtTextValue(cetStuPwd);

        if (AbStrUtil.isEmpty(mStuNo)) {
            ToastUtil.showMessage(this, "学号不能为空");
            return false;
        } else if (AbStrUtil.isEmpty(mPwd)) {
            ToastUtil.showMessage(this, "密码不能为空");
            return false;
        }
        return true;
    }

    /**
     * 自动登陆
     */
    public void autoLogin() {
        String username = sHelper.getString(Constant.STU_USERNAME);
        String pwd = sHelper.getString(Constant.STU_PASSWORD);
        if (username != null && pwd != null) {

            if (Constant.AUTO_LOGIN) {
                cedStuNo.setText(username);
                cetStuPwd.setText(pwd);
                login();
            } else {
                cedStuNo.setText(sHelper.getString(Constant.STU_USERNAME));
            }
        }
    }

    /**
     * 保存登陆的信息
     */
    private void saveLoginInfo(SchoolLogin m_login) {

        sHelper.putString(Constant.STU_USERNAME, mStuNo);
        sHelper.putString(Constant.STU_PASSWORD, mPwd);
        sHelper.putBoolean(Constant.STU_IS_LOGIN, true);
        sHelper.putString(Constant.STU_UID, m_login.getData().getUid());
        sHelper.putString(Constant.STU_TOKEN, m_login.getData().getAccess_token());
    }


    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.STU_LOGIN) && jo != null) {
            SchoolLogin m_login = new SchoolLogin(jo.toString());

            if (m_login != null) {


                if (m_login.getCode().equals("200")) {

                    ToastUtil.showMessage(this, "登录成功");
                    saveLoginInfo(m_login);
                    startActivity(new Intent(CurriculumScheduleLoginActivity.this, CurriculumSchedule1Activity.class));
                    finish();

                } else {
                    ToastUtil.showMessage(this, m_login.getMsg());
                }
            }

        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
